package sdk.webview.fmc.com.fmcsdk.view;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * 自定义签名View
 * @author Evan
 *
 */
public class PaintView extends View {

	/**
	 * 手写笔paint
	 */
	private Paint paint;
	
	/**
	 * 手写移动轨迹
	 */
	private Path path;
	private Canvas cacheCanvas;
	private Bitmap cachebBitmap;
	private int paintColor= Color.BLACK;
	private Bitmap bitmap;
	Context mContext;
	/**
	 * 初始化开关
	 */
	private boolean flag;
	
	/**
	 * 是否写过字
	 */
	private boolean mark;

	public PaintView(Context context) {
		super(context);
		mContext=context;
	}

	public PaintView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		mContext=context;
	}

	public PaintView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext=context;
	}

	public Bitmap getCachebBitmap() {
		return cachebBitmap;
	}

	public void clear() {

		if (cacheCanvas != null) {
			mark = false;
			cacheCanvas.drawPaint(paint);
			paint.setColor(paintColor);
			if(bitmap!=null){
				cachebBitmap=bitmap.copy(Config.ARGB_8888, true);
				cacheCanvas=new Canvas(getBitmap());
			}else {
				cacheCanvas.drawColor(Color.WHITE);
			}
			invalidate();
		}
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		setMeasuredDimension(getMeasuredWidth(), getMeasuredHeight());
	}

	private void init() {
		paint = new Paint();
		paint.setAntiAlias(true);
		paint.setStrokeWidth(5);
		paint.setStyle(Paint.Style.STROKE);
		paint.setColor(paintColor);
		path = new Path();
		if(cachebBitmap==null){
			cachebBitmap = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(), Config.ARGB_8888);
			cacheCanvas = new Canvas(cachebBitmap);
			cacheCanvas.drawColor(Color.WHITE);
		}else {
			cacheCanvas=new Canvas(getBitmap());
		}
		flag = true;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		if (!flag) {
			init();
		}
		canvas.drawBitmap(cachebBitmap, 0, 0, null);
		canvas.drawPath(path, paint);
	}

	private float cur_x, cur_y;

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		float x = event.getX();
		float y = event.getY();

		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN: {
			cur_x = x;
			cur_y = y;
			path.moveTo(cur_x, cur_y);
			break;
		}

		case MotionEvent.ACTION_MOVE: {
			path.quadTo(cur_x, cur_y, x, y);
			cur_x = x;
			cur_y = y;
			mark = true;
			break;
		}

		case MotionEvent.ACTION_UP: {
			cacheCanvas.drawPath(path, paint);
			path.reset();
			break;
		}
		}

		invalidate();

		return true;
	}

	public boolean isMark() {
		return mark;
	}

	private Bitmap getBitmap(){
		cachebBitmap=scaleBitmap(mContext, cachebBitmap, getMeasuredWidth(), cachebBitmap.getHeight()*getMeasuredWidth()/cachebBitmap.getWidth());
		return cachebBitmap;
	}

	public void setBitmap(Bitmap bitmap){
		cachebBitmap=bitmap.copy(Config.ARGB_8888, true);
		this.bitmap=bitmap;
	}
	public void setPaintColor(int color){
		paintColor=color;
	}
	  public static Bitmap scaleBitmap(Context context, Bitmap bitmap, float ww, float hh){
		  	try {
//		  		Bitmap bitmap = getCompressBitmap(context, pathName);
			  	if (bitmap != null) {
			  		int w = bitmap.getWidth();
					int h = bitmap.getHeight();
					Matrix matrix=new Matrix();
					float scaleW=ww/(float)w;
					float scaleH=hh/(float)h;
					matrix.postScale(scaleW, scaleH);
					bitmap= Bitmap.createBitmap(bitmap, 0, 0, w, h,matrix,true);
				}
				return bitmap;
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
		  
	  }

		public static Bitmap decodeFile(String path, Options opts){
			FileInputStream fis = null;
			Bitmap bitmap = null;
			try {
				fis = new FileInputStream(path);
				bitmap = BitmapFactory.decodeFileDescriptor(fis.getFD(), null, opts);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}finally{
				if (fis != null) {
					try {
						fis.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
			return bitmap;
		}
}